/**
 * 集群模块控制器
 * 
 * @author zhongfuhua
 */

var clusterApp = angular.module("clusterApp", ["clusterServiceApp", "clusterInstanceServiceApp"]);

clusterApp.controller("clusterCtrl", [
		"$scope",
		"commonService",
		"clusterService",
		"clusterInstanceService",
		"$timeout",
		"$log",
		function($scope, commonService, clusterService, clusterInstanceService, $timeout, $log) {
			
			
			// 表格
			$scope.$clusterTable = $('#clusterTable');
			
			// 按钮
			$scope.$clusterAddButton = $('#clusterAddButton');
			$scope.$clusterModButton = $('#clusterModButton');
			$scope.$clusterRemoveButton = $('#clusterRemoveButton');
			
			// 模态框
			$scope.$clusterModal = $('#clusterModal');
			
			// 表格选中实例对象
			$scope.selectionIds = new Array();
			$scope.selections = new Array();
			
			// 监视isSucce的状态
			$scope.action = null;
			$scope.isSuccess = true;
			$scope.error = null;
			
			// vo
			$scope.clusterVO = {};
			$scope.clusterInstanceVO = {};
			$scope.clusterInstanceTableCurrent = null;
			
			ffc.util.watchResult($scope, $timeout);
			
			
			/**
			 * 
			 * 功能说明:查询数据源
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.queryCluster = function(){
				$scope.$clusterTable.bootstrapTable("selectPage", 1);  
			}
			
			
			/**
			 * 
			 * 功能说明:新增修改集群
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.saveCluster = function(){
				if($scope.action=="MOD"){
					clusterService.update($scope.clusterVO, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						new MessageDialog().success("修改成功!!");
						$scope.$clusterModal.modal("hide");
						$scope.queryCluster();
					},function(error){
						$scope.error = error;
						$scope.isSuccess = false;
					});
				}else{
					clusterService.add($scope.clusterVO, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						new MessageDialog().success("添加成功!!");
						$scope.$clusterModal.modal("hide");
						$scope.queryCluster();
					},function(error){
						$scope.error = error;
						$scope.isSuccess = false;
					});
				}
			}
			
			/**
			 * 
			 * 功能说明:删除集群
			 * 
			 * @author ZHONGFUHUA
			 * 
			 * @Date 2016年10月25日
			 * 
			 */
			$scope.deleteCluster = function(){
				new MessageDialog().confirm("确定删除？", function() {
					clusterService.removeList($scope.selections, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						new MessageDialog().alert("删除成功");
						$scope.queryCluster();
					},function(error){
						$scope.error = error;
						$scope.isSuccess = false;
					});
			   });
			}
			
			
			$scope.queryClusterInstance = function(){
				if($scope.clusterInstanceTableCurrent != null &&  $scope.clusterInstanceTableCurrent != undefined){
					$scope.clusterInstanceTableCurrent.bootstrapTable("selectPage", 1);  
				}
			}
			
			
			$(function() {
				/**
				 * 
				 * 功能说明:初始化数据源表格
				 * 
				 * @author ZHONGFUHUA
				 * 
				 * @Date 2016年10月25日
				 * 
				 */
				$scope.$clusterTable.bootstrapTable({
					queryParams : function(params) {
						var selfParams = {
							name : $scope.clusterVO.queryName
						}
						return $.extend({}, params, selfParams);
					},
					ajax : function(render) {
						clusterService.queryPage(render.data, function(result) {
							if(!result.result){
								$scope.error = result.msgTitle;
								$scope.isSuccess = false;
								return false;
							}
							ffc.util.loadTableData(render, result);  
						},function(error){
							$scope.error = error;
							$scope.isSuccess = false;
						});
						$scope.$clusterRemoveButton.prop('disabled', true);
						$scope.$clusterModButton.prop('disabled', true);
					},
					onExpandRow : function(index, row, $detail){
						var clusterInstanceTable = $detail.html(
								"<div class=\"table-responsive\">" +
									"<table name='clusterInstanceTable'></table>" +
								"</div>"
								).find("table");
						
						var columns = [];
						
						columns.push({
							field : "instanceId",
							title : "实例ID",
							width : "15%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "ipAddress",
							title : "实例地址",
							width : "20%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "running",
							title : "运行",
							width : "10%",
							align : "center",
							classes : "table-column",
							valign : "middle",
							formatter : clusterInstanceRunningFormatter
						});
						
						
						
						$scope.clusterInstanceTableCurrent = clusterInstanceTable;
						
						clusterInstanceTable.bootstrapTable({
							idField : "id",
							sidePagination: "server",
							columns : columns,
							pagination : true,
							pageList : [5, 10, 20],
							detailView : false,
							queryParamsType : "",
							queryParams : function(params) {
								var selfParams = {
									clusterName : row.name
								};
								return $.extend({}, params, selfParams);
							},
							ajax : function(render) {
								clusterInstanceService.queryPage(render.data, function(result) {
									if(!result.result){
										$scope.error = result.msgTitle;
										$scope.isSuccess = false;
										return false;
									}
									ffc.util.loadTableData(render, result);  
								},function(error){
									$scope.error = error;
									$scope.isSuccess = false;
								});
							}
						});
					}
				});
				
				/**
				 * 
				 * 功能说明:每次选择时重新获取表格中被选中对象
				 * 
				 * @author ZHONGFUHUA
				 * 
				 * @Date 2016年10月25日
				 * 
				 */
				$scope.$clusterTable.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table',function () {
					$scope.$clusterModButton.prop('disabled', !$scope.$clusterTable.bootstrapTable('getSelections').length);
					$scope.$clusterRemoveButton.prop('disabled', !$scope.$clusterTable.bootstrapTable('getSelections').length);
					$scope.selectionIds = new Array();
					$scope.selections = new Array();
					angular.forEach($scope.$clusterTable.bootstrapTable('getSelections'), function(data, index, array) {
						$scope.selectionIds.push(data.id);
						$scope.selections.push(data);
					});
				})
				
												
				/**
				 * 
				 * 功能说明:每次选择时重新获取表格中被选中对象
				 * 
				 * @author ZHONGFUHUA
				 * 
				 * @Date 2016年10月25日
				 * 
				 */
				$scope.$clusterTable.on('load-success.bs.table', function (data) {
					$('[data-toggle="tooltip"]').tooltip();
				});
				
				
				/**
				 * 
				 * 功能说明:弹出框关闭事件
				 * 
				 * @author ZHONGFUHUA
				 * 
				 * @Date 2016年10月25日
				 * 
				 */
				$scope.$clusterModal.on("hidden.bs.modal",function(){
					$scope.action = null;
					$scope.clusterForm.$setPristine();
					var queryName = $scope.clusterVO.queryName;
					$scope.clusterVO = {queryName : queryName};
				});
				
				
				/**
				 * 
				 * 功能说明:弹出框打开事件
				 * 
				 * @author ZHONGFUHUA
				 * 
				 * @Date 2016年10月25日
				 * 
				 */
				$scope.$clusterModal.on('shown.bs.modal',function(){
					if($scope.action=="MOD"){
						if($scope.selections.length > 1){
							$scope.$clusterModal.modal("hide");
							new MessageDialog().warning("只能选择一条记录！");
							return false;
						} 
						$scope.clusterVO = angular.copy($scope.selections[0]);
						$("#clusterName").attr("readonly", "readonly");
						ffc.util.angularApply($scope, $timeout);
					}else{
						$("#clusterName").removeAttr("readonly", "readonly");
					}
				});
				
				
				/**
				 * 
				 * 功能说明:格式化表格输出
				 * 
				 * @author ZHONGFUHUA
				 * 
				 * @Date 2016年10月25日
				 * 
				 */
				clusterInstanceRunningFormatter = function(value, row, index) {
					if(value == 1){
						return "运行中";
					}else{
						return "已关闭"
					}
				}
				
				
			});
}]);
